
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="1000" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="业务类型" prop="bizType">
        <el-select clearable v-model="formData.bizType" placeholder="请选择">
          <el-option v-for="item in bizTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台" prop="platformId">
        <el-select clearable v-model="formData.platformId" placeholder="请选择">
          <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channelId">
        <el-select clearable v-model="formData.channelId" placeholder="请选择">
          <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="formData.channelId == 2" label="飞猪规则id" prop="fliggyRuleId">
        <el-input v-model="formData.fliggyRuleId" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="cityId">
        <el-select filterable clearable v-model="formData.cityId" placeholder="请选择" @change="handleCityChange">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否上线" prop="isOnline">
        <el-select v-model="formData.isOnline" placeholder="请选择" size="large">
          <el-option :value="0" label="下线"></el-option>
          <el-option :value="1" label="上线"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="兜底时间" prop="bagTime">
        <el-input v-model="formData.bagTime" placeholder="请输入兜底时间"></el-input>
      </el-form-item>
      <div>
        <el-form-item label="兜底供应商">
          <div class="flex_box" v-for="(item, i) in polyArr" :key="item.mathNum">
            <el-select style="width: 12vw" filterable clearable v-model="item.supplierId" placeholder="请选择" @change="handleCityChange">
              <el-option v-for="ele in supplierOptions" :key="ele.id" :label="ele.companyName" :value="ele.id" />
            </el-select>
            <el-select multiple style="width: 12vw" filterable clearable v-model="item.vehicleTypeIds" placeholder="请选择" @change="handleCityChange">
              <el-option v-for="ele in carTypeArr" :key="ele.value" :label="ele.label" :value="ele.value" />
            </el-select>
            <el-input style="width: 8vw; margin: 0 14px" placeholder="请输入" v-model="item.percent"></el-input>
            <el-button type="danger" @click="handleRemove(i)">删除</el-button>
            <el-button class="add_btn" v-if="i == polyArr.length - 1" type="primary" @click="handleAdd">新增</el-button>
          </div>
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import JntApi from "@/api/jnt";
import Supplier from "@/api/supplier";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";

const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref();
let formData = ref({
  id: "",
  bizType: null,
  platformId: null,
  channelId: null,
  cityId: null,
  cityName: null,
  isOnline: null,
  bagTime: null,
  fliggyRuleId: null,
});
const rules = reactive({
  bizType: [{ required: true, message: "请输入类型" }],
  platformId: [{ required: true, message: "请输入平台" }],
  channelId: [{ required: true, message: "请输入渠道" }],
  cityId: [{ required: true, message: "请选择城市!" }],
  isOnline: [{ required: true, message: "请输入是否上线!" }],
});
const polyArr: any = ref([{ supplierId: "", percent: "1", vehicleTypeIds: null, mathNum: Date.now() + Math.random().toString(16).slice(2) }]); //里程价
const ifShow = ref(false);
const modelTitle = ref();
const supplierOptions = ref();
const cityOptions = ref();
function edit(title: any, row: any) {
  console.log("点击了新增", title, row);

  modelTitle.value = title;
  ifShow.value = true;
  if (row && row.id) {
    // 里程价格
    let { bagSupplier } = row;

    if (bagSupplier && bagSupplier.length) {
      polyArr.value = JSON.parse(JSON.stringify(bagSupplier));
    }
  }
  if (title == "新增") {
    formData.value = {
      id: "",
      bizType: null,
      platformId: null,
      channelId: null,
      cityId: null,
      cityName: null,
      isOnline: null,
      bagTime: null,
      fliggyRuleId: null,
    };
  }
  Object.assign(formData.value, row);
}

function handleAdd() {
  polyArr.value.push({ supplierId: "", percent: "", vehicleTypeIds: null, mathNum: Date.now() + Math.random().toString(16).slice(2) });
}

function handleRemove(index: string) {
  if (polyArr.value.length !== 1) {
    polyArr.value.splice(index, 1);
  }
}
/**
 * 确定按钮点击事件
 */
async function handleOk() {
  if (formData.value.channelId == 2 && !formData.value.fliggyRuleId) {
    ElMessage.warning("飞猪规则id必填");
    return;
  }
  await formRef.value.validate((valid, fields) => {
    if (valid) {
      let model = formData.value;
      model.bagSupplier = polyArr.value;

      JntApi.cityConfigEdit(model).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

//城市下拉框点击事件
function handleCityChange(value: any) {
  formData.value.cityName = cityOptions.value.find((item: any) => item.id == value).name;
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  polyArr.value = [{ supplierId: "", percent: "1", vehicleTypeIds: null, mathNum: Date.now() + Math.random().toString(16).slice(2) }];
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
      supplierOptions.value.unshift({ companyName: "退回渠道", id: "0" });
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  margin-bottom: 14px;
  position: relative;
  .add_btn {
    position: absolute;
    right: -80px;
  }
}
</style>
